import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'HomeView',
    component: () => import('../views/HomeView.vue'),
    meta: { title: '首页' }
  },
  {
    path: '/detail/:id',
    name: 'DetailView',
    component: () => import('../views/DetailView.vue'),
    meta: { title: '详情页' }
  },
  {
    path: '/category/:id',
    name: 'CategoryView',
    component: () => import('../views/CategoryView.vue'),
    meta: { title: '分类' }
  },
  {
    path: '/announce',
    name: 'AnnounceView',
    component: () => import('../views/AnnounceView.vue'),
    meta: { title: '公告' }
  },
  {
    path: '/settings',
    name: 'Settings',
    component: () => import('@/views/SettingsView.vue')
  },
  {
    path: '/publish',
    name: 'PublishView',
    component: () => import('../views/PublishView.vue'),
    meta: { title: '发布图文' }
  },
  {
    path: '/user/:id',
    name: 'UserView',
    component: () => import('../views/UserView.vue'),
    meta: { title: '个人主页' }
  },
  {
    path: '/search',
    name: 'SearchView',
    component: () => import('../views/SearchView.vue'),
    meta: { title: '搜索结果' }
  },
  {
    path: '/feedback',
    name: 'FeedbackView',
    component: () => import('../views/FeedbackView.vue'),
    meta: { title: '用户反馈' }
  },

]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由前置守卫，设置页面标题
router.beforeEach((to, from, next) => {
  document.title = to.meta.title ? `${to.meta.title} - 图文发布系统` : '图文发布系统'
  next()
})

export default router